package bankingSystemServer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import bankingSystemServer.Transaction.TransactionType;

/**
 * holds account information and transaction history
 */
public class Account {
	private String creationDate;

	public String getCreationDate() {
		return creationDate;
	}

	private String accountNumber;
	private Customer customer;
	private int balance;
	Vector<Transaction> history;

	/**
	 * creates a new Account object
	 */
	public Account(String accountNumber, Customer customer, int balance) {
		history = new Vector<Transaction>();
		this.creationDate = generateCurrentDate();
		this.accountNumber = accountNumber;
		this.customer = customer;
		this.balance = balance;
	}
	
	public void addFunds(int funds){
		balance += funds;
	}
	
	public void subtractFunds(int funds){
		balance -= funds;
	}

	public Account(int accountNumber2, int balance2) {
		// TODO Auto-generated constructor stub
	}

	/**
	 * processes a transaction and adds it to the history
	 * 
	 * @param transaction
	 */
	public void addAndProcessTransaction(Transaction transaction) {
		TransactionType type = transaction.getType();
		if (type == Transaction.TransactionType.DEPOSIT) {
			balance += transaction.getAmount();
		} else if (type == Transaction.TransactionType.WITHDRAW) {
			balance -= transaction.getAmount();
		}
		history.add(transaction);
	}

	/**
	 * returns the balance
	 * 
	 * @return
	 */
	public int getBalance() {
		return balance;
	}

	/**
	 * returns the customer name
	 * 
	 * @return name
	 */
	public String getCustomerName() {
		return customer.getName();
	}

	/**
	 * returns the account number
	 * 
	 * @return the account number
	 */
	public String getAccountNumber() {
		return accountNumber;
	}

	/**
	 * returns the creation date
	 * 
	 * @return the creation data
	 */
	public String generateCurrentDate() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}

	public AccountInfo getAccountInfo() {
		return new AccountInfo(accountNumber, balance, customer.getName());
	}
}
